﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.BLL.DocManger;
using Frame.Lib.Core.Right;
using Frame.Lib.Common;
using Frame.Lib.Enums;
using Frame.Lib.Utilities;


namespace Jzy.Web.DocManager.Document
{
    public partial class MoveDlg : System.Web.UI.Page
    {
        private DirBO _dir;
        private DirBO Dir
        {
            get
            {
                if (_dir == null)
                {
                    _dir = new DirBO();
                }
                return _dir;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTree();
            }
            AddAttr();
        }

        private void AddAttr()
        {
            tvList.Attributes.Add("onclick", "SingleSelect();");
        }

        private void BindTree()
        {
            if (AppSession.UserID == Constants.ADMIN_ID)
            {
                Dir.BindTreeView(tvList, null,true);
            }
            else
            {
                RecordRightBO right = new RecordRightBO("DocDirRight");
                List<int> lstDirIDs = right.GetBindIDs(AppSession.UserID, RecordRightName.Browse, RightOwnerType.User);
                Dir.BindTreeView(tvList, lstDirIDs,true);

                //对有增加权限的加上CheckBox
                List<int> lstAddIDs = right.GetBindIDs(AppSession.UserID, RecordRightName.Add, RightOwnerType.User);
                if (tvList.Nodes.Count > 0)
                {
                    SetCheckBox(tvList.Nodes[0], lstAddIDs);

                    //给“我的文档”加上checkbox
                    SetCheckBox(tvList.Nodes[1], null);
                }

                

            }
        }

        private void SetCheckBox(TreeNode treeNode, List<int> lstAddIDs)
        {
            int id = int.Parse(treeNode.Value);
            if (lstAddIDs != null)
            {
                if (lstAddIDs.IndexOf(id) >= 0)
                {
                    treeNode.ShowCheckBox = true;
                }
            }
            else
            {
                treeNode.ShowCheckBox = true;
            }

            foreach (TreeNode node in treeNode.ChildNodes)
            {
                SetCheckBox(node, lstAddIDs);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            if (tvList.CheckedNodes.Count > 0)
            {
                int dirID = Convert.ToInt32(tvList.CheckedNodes[0].Value);

                ////移动目录
                //string strSelectDirIDs = Request["SelectDirIDs"];
                //List<int> lstSelectDirIDs = StrUtil.SplitToInt(strSelectDirIDs);
                //if (Dir.Include(lstSelectDirIDs, dirID))
                //{
                //    JSUtil.Alert("目标目录不得包括自己及子目录");
                //    return;
                //}
                //Dir.Move(lstSelectDirIDs, dirID);
                //string strScript = "";

                //移动文档
                string strSelectDocIDs = Request["SelectDocIDs"];
                List<int> lstSelectDocIDs = StrUtil.SplitToInt(strSelectDocIDs);
                DocumentBO doc = new DocumentBO();
                doc.Move(lstSelectDocIDs, dirID);

                string strScript = "close();returnValue=true";
                JSUtil.ResponseJS(strScript);
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            JSUtil.ResponseJS("close();returnValue=false");
        }
    }
}
